// pages/allMajor/allMajor.js
const app = getApp();
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    schoolId: '',
    list: [],
    pageNumber: 1,
    pageSize: 0,
    totalPage: 0,
    lastPage: true,
    showFilter: false,
    filter: {},
    schoolArray: [],
    departmentArray: [],
    categoryArray: []
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    self.init();
    self.getSchools();
  },
  init() {
    var url = host + '/lx/mini/school/major';
    var params = {
      schoolId: self.data.schoolId,
      filter: JSON.stringify(self.data.filter)
    };
    if (self.data.keyword) {
      params.keyword = self.data.keyword;
    }
    var success = function (res) {
      console.log(res);
      self.setData({
        list: res.info.list,
        pageNumber: res.info.pageNumber,
        pageSize: res.info.pageSize,
        totalPage: res.info.totalPage,
        lastPage: res.info.lastPage
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  getSchools() {
    var url = host + '/lx/mini/school/list';
    var params = {
    };
    var success = function (res) {
      console.log(res);
      self.setData({
        schoolArray: res.info.list
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  getDepartments() { // 暂时废弃
    var url = host + '/lx/mini/school/departments';
    var params = {
      schoolId: self.data.schoolId
    };
    var success = function (res) {
      self.setData({
        departmentArray: res.info
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  goInfo(event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/app/schools/majorInfo/majorInfo?majorId=' + id
    })
  },
  search(event) {
    // var keyword = event.detail.value;
    // self.setData({
    //   keyword: keyword
    // })
    self.init();
  },
  clearKeyword() {
    self.setData({
      keyword: ''
    })
    self.init();
  },
  showFilter(event) {
    self.setData({
      showFilter: !self.data.showFilter
    })
  },
  selectRank(e) {
    var rank = Number(e.currentTarget.dataset.rank);
    var filter = self.data.filter;
    if (filter.rank === rank) {
      filter.rank = '';
    } else {
      filter.rank = rank;
    }
    filter.category = '';
    self.setData({
      filter: filter,
      categoryArray: []
    })

    if (filter.department) {
      var url = host + '/lx/mini/school/categories';
      var params = {
        schoolId: self.data.schoolId,
        department: filter.department
      };
      if (filter.rank || filter.rank === 0) {
        params.rank = filter.rank;
      }
      var success = function (res) {
        self.setData({
          categoryArray: res.info
        })
      }
      var fail = function () { };
      var complete = function () { };
      utils.request(url, params, success, fail, complete);
    }
  },
  selectSchool(e) {
    var schoolId = e.currentTarget.dataset.schoolid;
    var filter = self.data.filter;
    if (self.data.schoolId === schoolId) {
      self.setData({
        schoolId: ''
      })
    } else {
      self.setData({
        schoolId: schoolId
      })
    }
    filter.department = '';
    filter.category = '';
    self.setData({
      filter: filter,
      departmentArray: [],
      categoryArray: []
    })

    if (self.data.schoolId) {
      var url = host + '/lx/mini/school/departments';
      var params = {
        schoolId: self.data.schoolId
      };
      if (filter.rank || filter.rank === 0) {
        params.rank = filter.rank;
      }
      var success = function (res) {
        self.setData({
          departmentArray: res.info
        })
      }
      var fail = function () { };
      var complete = function () { };
      utils.request(url, params, success, fail, complete);
    }
  },
  selectDepartment(e) {
    var department = e.currentTarget.dataset.department;
    var filter = self.data.filter;
    if (filter.department === department) {
      filter.department = '';
    } else {
      filter.department = department;
    }
    filter.category = '';
    self.setData({
      filter: filter,
      categoryArray: []
    })

    if (filter.department) {
      var url = host + '/lx/mini/school/categories';
      var params = {
        schoolId: self.data.schoolId,
        department: filter.department
      };
      if (filter.rank || filter.rank === 0) {
        params.rank = filter.rank;
      }
      var success = function (res) {
        self.setData({
          categoryArray: res.info
        })
      }
      var fail = function () { };
      var complete = function () { };
      utils.request(url, params, success, fail, complete);
    }
  },
  selectCategory(e) {
    var category = e.currentTarget.dataset.category;
    var filter = self.data.filter;
    if (filter.category === category) {
      filter.category = '';
    } else {
      filter.category = category;
    }
    self.setData({
      filter: filter
    })
  },
  goFilter(e) {
    console.log(self.data.filter);
    var url = host + '/lx/mini/school/major';
    var params = {
      schoolId: self.data.schoolId,
      filter: JSON.stringify(self.data.filter)
    };
    if (self.data.keyword) {
      params.keyword = self.data.keyword;
    }
    var success = function (res) {
      self.setData({
        list: res.info.list,
        pageNumber: res.info.pageNumber,
        pageSize: res.info.pageSize,
        totalPage: res.info.totalPage,
        lastPage: res.info.lastPage
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);

    self.setData({
      showFilter: false
    })
  },
  onReachBottom() {
    if (self.data.lastPage) {
      return;
    }
    var url = host + '/lx/mini/school/major';
    var params = {
      schoolId: self.data.schoolId,
      filter: JSON.stringify(self.data.filter),
      page: self.data.pageNumber + 1
    };
    if (self.data.keyword) {
      params.keyword = self.data.keyword;
    }
    var success = function (res) {
      console.log(res);
      var list = self.data.list.concat(res.info.list);
      self.setData({
        list: list,
        pageNumber: res.info.pageNumber,
        pageSize: res.info.pageSize,
        totalPage: res.info.totalPage,
        lastPage: res.info.lastPage
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  onKeywordChange(e) {
    self.setData({
      keyword: e.detail.value
    })
  },
  goHome(e) {
    wx.reLaunch({
      url: '/pages/app/schools/index/index'
    })
  }
})